<?php

//display news

class Controller extends controllerBase {

	public $method = array(0 => 'view');

	public function view($id = "")
	{
		if($id != "")
		{
			#display a single entry (with comment form if logged in)
			
			#try to find it by ID
			$query = new Query("SELECT `id` FROM `news` WHERE `id`  = '" . $id ."' ORDER BY `id` LIMIT 0, 1");
			
			#if not found by ID, try to find by url
			if($query->num_rows() <= 0)
			{
				#we passed the url of the news entry, see if it exists and find the id
				$query = new Query("SELECT `id` FROM `news` WHERE `url` = '" . $id ."' ORDER BY `id` LIMIT 0, 1");
			}
			
			#now we actually run the query
			
			
			#if the item exits, display it, else, error
			if($query->num_rows() > 0)
			{
				#the item exits, get it and display
				$p = $query->fetch_row();
				
				$news = new news($p[0]);
				
				#display the news entry
				echo '<h2>' . $news->title . '</h2>
					  <h3 class="date">' . format::date($news->data['date']) . '</h3>
					  '. format::content($news->content);  
			}
			else
			{
				echo display::itemDNE('news entry');
			}
		}
		else
		{
			#display the first X entreis, really, this is the page method
			self::page(1);
		}
	}
	

	public function page($page)
	{
		#displays a page of entries	
		include(MODULE_DIR . 'news/config.php');
		echo news_module::page($page, $config['modules']['news']['items-per-page']);
		
	}
	
	//display all the news entries for a specific month
	public function archive($year = 0, $month = 0)
	{
		include(MODULE_DIR . 'news/config.php');
		
		if($year == 0)	//no year was set, this years archives + previous years
		{
			echo news_module::archiveNews();
			
			//get all years
			echo news_module::archiveYears();
		}
		else
		{		
			if($month ==  0)
			{
				//no month is set, grab all dates for that year
				echo news_module::archive($year, 0);
			}
			else
			{
				// a specific month is set
				echo news_module::archive($year, $month);
			}
		}
	}
	
	
}
?>